{extend name="public/base"}

{block name="title"}
绑定手机号
{/block}

{block name="style"}
{css href="/static/wechat/css/set.css" /}
{/block}

{block name="body"}
<div class="pageControl setTelephone">
    <div class="msui_nav msui_navTop">
        <a class="msui_navTop-back" href="{:url('index')}"><i class="micon-back"></i></a>
        <h1 class="msui_navTop-title">绑定手机号</h1>
    </div>
    <div class="weui-top">
        <form>
            <div class="weui-cells weui-cells_form">
                <div class="weui-cell">
                    <div class="weui-cell__hd"><label class="weui-label">手机号码</label></div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="tel" name="phone"  placeholder="请输入手机号码">
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="weui-cell__hd"><label class="weui-label">验证码</label></div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="number" name="code" placeholder="请输入验证码">
                    </div>
                    <div class="weui-cell_ft">
                        <button class="weui_cell-verify js-verify cl-orange" >获取验证码</button>
                    </div>
                </div>
            </div>
            <div class="msui_button">
                <div class="msui_button-bd">
                    <a href="javascript:;" class="msui_button-content">确认修改</a>
                </div>
            </div>
        </form>
    </div>

</div>

<!-- loading toast -->
<div id="loadingToast" style="display:none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast" style="opacity: 1; visibility: visible;">
        <i class="weui-loading weui-icon_toast"></i>
        <p class="weui-toast__content">加载中...</p>
    </div>
</div>

{/block}

{block name="javascript"}
<script>
    $(function () {
        var obj = {
            is_continue : true
        };
        $(".msui_button-content").click(function () {
            $('#loadingToast').fadeIn();
            var code = $("input[name='code']").val();
            var phone = $("input[name='phone']").val();
            // 先检查帐号是否已绑定微信
            $.get('{:url("check_phone_is_band")}', {phone : phone}, function (msg) {
                if (msg.code == 200) {
                    obj.is_continue = false;

                    if (confirm('该手机号已绑定微信，是否更换为新的微信号')) {
                        obj.is_continue = true;
                    }
                }
            }, 'json');
            if (!obj.is_continue) {
                return false;
            }
            $.ajax({
                type : 'POST',
                url : '?',
                data : {
                    type : 2,
                    code : code,
                    phone : phone
                },
                dataType : 'json',
                success : function (msg) {
                    $('#loadingToast').fadeOut();
                    if (msg.code == 200) {
                        window.location.href = msg.url;
                    } else {
                        var code = msg.code;
                        $.alert(error_code[code]);
                    }
                }
            });
        });
    });
</script>
{/block}